package com.mall.order.biz.handler;/**
 * Created by ciggar on 2019/8/1.
 */

import com.mall.commons.tool.exception.BizException;
import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dal.entitys.OrderShipping;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.user.IAddressService;
import com.mall.user.dto.AddressDetailRequest;
import com.mall.user.dto.AddressDetailResponse;
import com.mall.user.dto.AddressDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import sun.font.CreatedFontTracker;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.UUID;

/**
 *  ciggar
 * create-date: 2019/8/1-下午5:06
 *
 * 处理物流信息（商品寄送的地址）
 */
@Slf4j
@Component
public class LogisticalHandler extends AbstractTransHandler {

    @Reference(check = false)
    IAddressService addressService;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    @Transactional
    public boolean handle(TransHandlerContext context) {
        // 取出和地址有关信息
        CreateOrderContext orderContext = (CreateOrderContext) context;
        Long addressId = orderContext.getAddressId();
        String orderId = orderContext.getOrderId();

        // 去地址表查出地址信息
        AddressDto addressDto = getAddressDto(addressId);

        // 去order_shipping表插入新的信息
        int effectRow = insert2OrderShipping(orderId, addressDto);

        return effectRow == 1;
    }

    private AddressDto getAddressDto(Long addressId) {
        AddressDetailRequest addressDetailRequest = new AddressDetailRequest();
        addressDetailRequest.setAddressId(addressId);
        AddressDetailResponse addressDetailResponse = addressService.addressDetail(addressDetailRequest);
        AddressDto addressDto = addressDetailResponse.getAddressDto();
        return addressDto;
    }

    private int insert2OrderShipping(String orderId, AddressDto addressDto) {
        OrderShipping orderShipping = new OrderShipping();
        orderShipping.setOrderId(orderId);
        orderShipping.setCreated(new Date());
        orderShipping.setUpdated(new Date());
        String streetName = addressDto.getStreetName();
        String[] address = streetName.split("-");
        orderShipping.setReceiverState(address[0]);
        orderShipping.setReceiverCity(address[1]);
        orderShipping.setReceiverDistrict(address[2]);
        orderShipping.setReceiverAddress(address[3]);
        orderShipping.setReceiverPhone(addressDto.getTel());
        orderShipping.setReceiverMobile(addressDto.getTel());
        orderShipping.setReceiverName(addressDto.getUserName());
        int effectRow = orderShippingMapper.insert(orderShipping);
        return effectRow;
    }
}
